﻿using System;
using RpcHelper;
using RpcModel;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Helper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
    internal class ResourceShieldDAL : SqlBasicClass
    {
        public ResourceShieldDAL() : base("ResourceShield", "RpcExtendService")
        {

        }
        public long Add(ResourceShield add)
        {
            base.Insert(add, out long id);
            return id;
        }

        internal bool CheckIsShieId(string shieKey, string resourcePath)
        {
            return base.CheckIsExists(new ISqlWhere[] {
                new SqlWhere("ShieKey", System.Data.SqlDbType.VarChar,36){Value=shieKey},
                new SqlWhere("ResourcePath", System.Data.SqlDbType.VarChar,100){Value=resourcePath}
            });
        }

        public ResourceShield Get(Guid resourceId)
        {
            return base.GetRow<Guid, ResourceShield>("ResourceId", resourceId);
        }
        public ResourceShield Get(long id)
        {
            return base.GetRow<long, ResourceShield>("Id", id);
        }
        public void SetBeOverdueTime(long id, long beOverdueTime)
        {
            if (!base.Update(new ISqlSetColumn[]
            {
                new SqlSetColumn("BeOverdueTime", System.Data.SqlDbType.BigInt){Value=beOverdueTime}
            }, "Id", id))
            {
                throw new ErrorException("rpc.store.shieId.set.fail");
            }
        }
        public Guid[] CheckIsShieId(Guid[] resourceId)
        {
            return base.GetOutColumn<Guid, Guid>("ResourceId", resourceId, "ResourceId");
        }

        internal void Delete(long id)
        {
            if (!base.Drop("Id", id))
            {
                throw new ErrorException("rpc.store.shieId.delete.fail");
            }
        }

        internal ResourceShield[] Query(ShieIdQueryParam query, IBasicPage paging, out long count)
        {
            paging.InitOrderBy("Id", true);
            return base.Query<ResourceShield>(paging.OrderBy, paging.Index, paging.Size, out count, query.ToWhere());
        }
    }
}
